<?php
/**
* Sep 17, 2011 - trungdt - Rewrite this action to speed up download file
*/
	// immanuel192 write this module to manage filedownload
	require_once('exponent.php');
	ob_end_clean();
	ob_start("ob_gzhandler");
	$id=intval($_GET['id']);
	$file = $db->selectObject('file','id='. $id);    
	$db->increment('file','accesscount',1,"id = {$id}");

	$filenametest = $file->directory . "/" . $file->filename;

	if (file_exists($filenametest)) {			

		// NO buffering from here on out or things break unexpectedly. - RAM

		// This code was lifted from phpMyAdmin, but this is Open Source, right?
		// 'application/octet-stream' is the registered IANA type but
		//        MSIE and Opera seems to prefer 'application/octetstream'
		// It seems that other headers I've added make IE prefer octet-stream again. - RAM
		/*
		$mime_type = (EXPONENT_USER_BROWSER == 'IE' || EXPONENT_USER_BROWSER == 'OPERA') ? 'application/octet-stream;' :  $file->mimetype;

		header('Content-Type: ' . $mime_type);
		header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
		header("Content-length: ".filesize($filenametest));
		header('Content-Transfer-Encoding: binary');
		header('Content-Encoding:');
		header('Content-Disposition: attachment; filename="' . $file->filename . '"');
		// IE need specific headers
		if (EXPONENT_USER_BROWSER == 'IE') {
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Pragma: public');
			header('Vary: User-Agent');
		} else {
			header('Pragma: no-cache');
		}
		//Read the file out directly
		readfile($filenametest);
		exit();*/
		header("Location: ". BASE_URL."/{$filenametest}");
	} else {
		echo SITE_404_HTML;
	}

?>
